﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Faust.Andrew.MTG.Player
{    
    /// <summary>
	/// Interaction logic for ControlBox.xaml
	/// </summary>
	public partial class ControlBox : UserControl, IControlsView
	{
        static ControlBox()
        {
            PresenterProperty = DependencyProperty.Register("Presenter", typeof(GamePresenter), typeof(ControlBox));
        }

        static DependencyProperty PresenterProperty;
        public GamePresenter Presenter
        {
            get
            {
                return (GamePresenter)base.GetValue(PresenterProperty);
            }
            set
            {
                base.SetValue(PresenterProperty, value);
            }
        }

		public ControlBox()
		{
			this.InitializeComponent();
		}

        private void btnTokenCreature_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.AddToken();
        }

        private void btnRollDie_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            string sideString = InputBox.ShowInput("How many sides on the die?", "20");
            int sides;
            if (!Int32.TryParse(sideString, out sides) || sides < 1)
            {
                MessageBox.Show("You must provide a quantity > 1");
                return;
            }

            Presenter.Model.RollDie(sides);
        }

        private void btnEndTurn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.EndTurn();

        }

        private void AddExistingToken_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!(sender is FrameworkElement)) { return; }
            FrameworkElement element = (FrameworkElement)sender;

            if (!(element.DataContext is Token)) { return; }

            Presenter.Model.PlayTokenCreature(element.DataContext as Token, 1);
        }

        private void btnUntapAll_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.UntapAll();
        }

        private void btnNewCounter_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //string counterName = InputBox.ShowInput("What would you like to name the new counter?", "Other");

            //if (!String.IsNullOrEmpty(counterName))
            //{
            //    Presenter.Model.AddCounterType(
            //        new Faust.Andrew.MTG.Player.Shared.Counter() 
            //        { CounterType = Faust.Andrew.MTG.Player.Shared.CounterType.Other, 
            //            Value = 1, 
            //            Name = counterName 
            //        }
            //    );               
            //}
        }

        private void Counter_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Presenter.PreviewMouseLeftButtonDown((FrameworkElement)sender, e, true);
        }

        private void Counter_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Presenter.PreviewMouseMove((FrameworkElement)sender, e, this);
        }
	}
}